package com.anolesoft.twelvePlan.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.twelvePlan.entity.MesPlanGradeDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * @author zyq
 * @date 2022/5/25
 */
@Mapper
public interface MesPlanGradeMapper {
    @SelectProvider(type = createSQL.class, method = "queryPlanGradeSQL")
    List<MesPlanGradeDO> queryPlans(@Param("mesPlanGradeDO") MesPlanGradeDO mesPlanGradeDO, Page<MesPlanGradeDO> page);
    public static class createSQL {
        public static String queryPlanGradeSQL(@Param("mesPlanGradeDO") MesPlanGradeDO mesPlanGradeDO, Page<MesPlanGradeDO> page){
            return new SQL(){
                {
                    StringBuffer sql = new StringBuffer();
                    sql.append(" a.objuid, a.plan_grade, a.factory_id from mes_plan_grade a");
                    sql.append(" where a.factory_id = #{mesPlanGradeDO.factoryId}");
                    if (!StrUtil.isEmptyOrUndefined(mesPlanGradeDO.getPlanGrade())) {
                        sql.append(" and a.plan_grade = #{mesPlanGradeDO.planGrade}");
                    }
                    SELECT(sql.toString());
                    System.out.println("queryPlanGradeSQL:" + sql.toString());
                }
            }.toString();
        }
    }

    @Select("select count(*) from mes_plan_grade a where a.plan_grade = #{mesPlanGradeDO.planGrade} " +
            "and a.factory_id = #{mesPlanGradeDO.factoryId}")
    int repeatPlanGrade(@Param("mesPlanGradeDO") MesPlanGradeDO mesPlanGradeDO);

    @Insert("insert into mes_plan_grade(plan_grade, factory_id, create_id, create_time) values " +
            "(#{mesPlanGradeDO.planGrade},#{mesPlanGradeDO.factoryId},#{mesPlanGradeDO.createId},sysdate)")
    void insertPlanGrade(@Param("mesPlanGradeDO") MesPlanGradeDO mesPlanGradeDO);

    @Delete("delete from mes_plan_grade a where a.objuid = #{objuid}")
    void deletePlanGrade(@Param("objuid") String objuid);

    @Select("select a.objuid, a.plan_grade from mes_plan_grade a where a.factory_id = #{factoryId}")
    List<MesPlanGradeDO> queryForCheck(String factoryId);

    @Select("select * from mes_plan_grade a where a.objuid = #{objuid}")
    MesPlanGradeDO queryGradeName(String objuid);
}
